package com.component.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.component.domain.Channel;
import com.component.domain.MChannel;
import com.component.mapper.MChannelMapper;
import com.component.service.MChannelService;
import com.system.security.SecurityUtil;
import com.system.security.domain.SelfUserEntity;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
*
*/
@Service
@CacheConfig(cacheNames = "MChannelServiceImpl")
public class MChannelServiceImpl extends ServiceImpl<MChannelMapper, MChannel>
implements MChannelService {

    @Override
    public int getMchannelChildsCountById(Serializable id) {
        LambdaQueryWrapper<MChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MChannel::getPid,id);
        Integer count=this.baseMapper.selectCount(queryWrapper);
        return count;
    }

    @Cacheable(key = "#id")
    @Override
    public MChannel getById(Serializable id)
    {
        MChannel mChannel=super.getById(id);
        if(mChannel==null){
            List<MChannel> mChannelList=this.getSystemPriChannel();
            mChannel=mChannelList.stream().filter(e->e.getId().toString().equals(String.valueOf(id))).findFirst().orElse(null);
        }
        return mChannel;
    }

    @Override
    public List<MChannel> getTreeArray() {
        LambdaQueryWrapper<MChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MChannel::getIsprivate,false);
        List<MChannel> list=this.baseMapper.selectList(lambdaQueryWrapper);
        List<MChannel> array=new ArrayList<>();
        for (MChannel mChannel : list) {
            if(mChannel.getPid()!=null && mChannel.getPid()==0){
                getTreeArray(mChannel,list);
                array.add(mChannel);
            }
        }
        return array;
    }

    @Override
    public List<MChannel> getPriTreeArray(boolean f) {
        List<MChannel> maps=new ArrayList<>();
        if(f){
            maps.addAll(getSystemPriChannel());
        }
        SelfUserEntity userEntity=SecurityUtil.getSelfUserEntity();
        LambdaQueryWrapper<MChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MChannel::getIsprivate,true);
        lambdaQueryWrapper.eq(MChannel::getUserid,userEntity.getUserId());
        List<MChannel> list=this.baseMapper.selectList(lambdaQueryWrapper);
        maps.addAll(list);
        List<MChannel> array=new ArrayList<>();
        for (MChannel mChannel : maps) {
            if(mChannel.getPid()!=null && mChannel.getPid()==0){
                getTreeArray(mChannel,list);
                array.add(mChannel);
            }
        }
        return array;
    }

    @Override
    public List<MChannel> getSystemModelChannel() {
        SelfUserEntity userEntity=SecurityUtil.getSelfUserEntity();
        LambdaQueryWrapper<MChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MChannel::getIsprivate,true);
        lambdaQueryWrapper.eq(MChannel::getUserid,userEntity.getUserId());
        lambdaQueryWrapper.isNotNull(MChannel::getModelId);
        List<MChannel> list=this.baseMapper.selectList(lambdaQueryWrapper);
        List<MChannel> array=new ArrayList<>();
        for (MChannel mChannel : list) {
            if(mChannel.getPid()!=null && mChannel.getPid()==0){
                getTreeArray(mChannel,list);
                array.add(mChannel);
            }
        }
        return array;
    }

    @Override
    public List<MChannel> getAllTreeArray() {
        SelfUserEntity userEntity=SecurityUtil.getSelfUserEntity();
        LambdaQueryWrapper<MChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MChannel::getIsprivate,false);
        lambdaQueryWrapper.or(
                query->query.eq(MChannel::getIsprivate,true)
                        .eq(MChannel::getUserid,userEntity.getUserId().toString()));
        lambdaQueryWrapper.eq(MChannel::getIsprivate,true);
        lambdaQueryWrapper.eq(MChannel::getUserid,userEntity.getUserId());
        List<MChannel> list=this.baseMapper.selectList(lambdaQueryWrapper);
        List<MChannel> array=new ArrayList<>();
        for (MChannel mChannel : list) {
            if(mChannel.getPid()!=null && mChannel.getPid()==0){
                getTreeArray(mChannel,list);
                array.add(mChannel);
            }
        }
        return array;
    }

    @Cacheable(value = "getMChannelByPath",key = "#path")
    @Override
    public MChannel getMChannelByPath(String path) {
        LambdaQueryWrapper<MChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MChannel::getChannelpath,path);
        MChannel mChannel=this.baseMapper.selectOne(queryWrapper);
        if(mChannel == null){
            List<MChannel> list=this.getPriTreeArray(true);
            mChannel=list.stream().filter(e->e.getChannelpath().equals(path)).findFirst().orElse(null);
        }
        return mChannel;
    }

    @Override
    public List<MChannel> getMChannelByIds(List<String> ids) {
        LambdaQueryWrapper<MChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(MChannel::getId,ids);
        List<MChannel> mChannels=this.baseMapper.selectList(queryWrapper);
        return mChannels;
    }

    @Override
    public List<MChannel> getHotMChannels() {
        LambdaQueryWrapper<MChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MChannel::getIsprivate,false);
        List<MChannel> mChannels = this.baseMapper.selectList(queryWrapper);
        return mChannels;
    }

    private void getTreeArray(MChannel mChannel,List<MChannel> list){
        for (MChannel m : list){
            if(m.getPid()!=null && mChannel.getId().equals(m.getPid())){
                if(mChannel.getChilds()==null){
                    mChannel.setChilds(new ArrayList<>());
                }
                mChannel.getChilds().add(m);
                getTreeArray(m,list);
            }
        }
    }

    /**
     * 获取系统私有的栏目
     * @return
     */
    public List<MChannel> getSystemPriChannel(){
        List<MChannel> list = new ArrayList<>();
        MChannel mChannel_db=new MChannel(-1,"待办","db_path",0);
        MChannel mChannel_yb=new MChannel(-2,"已办","yb_path",0);
        list.add(mChannel_db);
        list.add(mChannel_yb);
        return list;
    }




}
